﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef GameWorld_h__
#define GameWorld_h__

#include <memory>
#include <stack>
#include "../base/Debuger.h"
#include "../utility/SmartPtrDef.h"
#include "../utility/Singleton.h"

namespace SkyEngine2d
{
	class Application;
	class IApplicationView;
	class Scene;
	class IRender;
	class DrawNode;
	class Application;
	class IDirectRender;
	class Scheduler;
	class Scene;
	class ActionManager;
	class TextureCache;
	class UiCore;
	class SpriteFrameCache;
	class EventManager;
	class AudioManager;
	
	/*!
	 \brief 游戏世界
	 \details 该类时引擎各个模块的中介者，也是游戏引擎的核心。可以通过该类获取其他各个模块的实例。
	*/
	class SGE_API GameWorld
		:public Singleton<GameWorld,false>
	{
	public:
	
		//！引擎的运行状态
		enum class EngineState
		{
			running,	/*!< 运行 */
			pause,		/*!< 暂停 */
			destroy		/*!< 销毁 */
		};
	
	public:
		//! 析构
		~GameWorld();
	
		//! 获取引擎状态
		const EngineState& getWorldState()const { return m_worldState; }
	
		//! 获取程序视图接口
		IApplicationView* getAppView()const { return m_appView; }
	
		//! 获取纹理缓存
		TextureCache* getTextureCache()const { return m_textureCache; }
	
		//! 获取D3d接口
		IDirectRender* getDirectInterface() const;
	
		//! 获取渲染接口
		IRender* getRenderInterface() const { return m_renderInterface; }
	
		//! 获取应用程序实例
		Application* getApplication() const { return m_app; }
	
		//! 获取调度器 
		Scheduler* getScheduler()const { return m_Scheduler; }
	
		//! 获取动画管理器
		ActionManager* getActionManger()const { return m_actionManager; }
	
		//! 获取uiCore 
		UiCore* getUiCore()const { return m_uiCore; }
	
		//! 获取音频管理器
		AudioManager* getAudioManager()const { return m_audioManager; }
	
		//! 获取默认的精灵帧缓存
		SpriteFrameCache* getDefultSpriteFrameCache()const;
	
		//! 获取事件管理器
		EventManager* getEventManager()const { return m_eventManager; }
	
		//! 压入场景 
		/*!
		\param const std::shared_ptr<Scene>& scene 场景
		\return bool 
		*/
		bool pushScene(const std::shared_ptr<Scene>& scene);
		
		//! 弹出一个场景
		/*!
		\return void  
		*/
		void popScene();
		
		//! 替换当前在运行的场景
		/*!
		\param const std::shared_ptr<Scene> & scene 
		\return void  
		*/
		void replaceScene(const std::shared_ptr<Scene>& scene);
	
		//! 获取当前正在运行的场景
		/*!
		\return Scene*  
		*/
		Scene* getRunningScene()const { return m_runningScene; }
	
		//! 开始运行世界
		void start();
	
		//! 设置时间缩放 
		/*!
		\param float scale 缩放系数  默认为 1
		\return void 
		*/
		void setTimeScale(float scale) { m_timeScale = scale; }
	
		//! 
		/*!
		\return float 获取当前的缩放系数
		*/
		float getTimeScale()const { return m_timeScale; }
	
		//! 打开控制台窗口，以显示调试信息
		/*!
		\return void
		*/
		void  openDebugConsole();

		//! 是否显示渲染信息，引擎默认为不显示
		/*!
		\param bool display
		\return void
		*/
		void  isDisplayRenderInfo(bool display);

	protected:
		//! 构造 
		GameWorld();
		//! 渲染场景 
		void drawScene();
		//Render
		void renderScene();
		bool init(IDirectRender* d3d, IRender* render, IApplicationView* app_view, Application* app);
		void end();
		//主循环
		void mainLoop(double dt);
	private:
		void displayRenderInfo();
		int fps(float dt);
	private:
		//当前正在运行的场景
		Scene* m_runningScene;
		//应用程序的视图
		IApplicationView* m_appView;
		//游戏状态
		EngineState   m_worldState;
		//应用程序实例
		Application* m_app;
		//d3d接口
		IDirectRender* m_d3dInterface;
		//渲染接口
		IRender* m_renderInterface;
		//调度器
		Scheduler* m_Scheduler;
		//场景栈
		std::stack<std::shared_ptr<Scene>> m_sceneStack;
		//动画管理器
		ActionManager* m_actionManager;
		//时间的缩放
		float  m_timeScale;
		//纹理缓存
		TextureCache* m_textureCache;
		//Ui核心
		UiCore* m_uiCore;
		//默认精灵帧缓存
		SpriteFrameCache* m_defaultSpriteFrameCache;
		//事件管理器
		EventManager* m_eventManager;
		//音频管理
		AudioManager* m_audioManager;
		//调试窗口
		bool m_isOpenDebugConsole;
		bool m_isDisplayRenderInfo;
		//帧率
		int m_fps;

#ifdef _DEBUG
		//Debug显示
		Text* m_debugInfoFps;
		Text* m_debugInfoVertexCount;
		Text* m_debugInfoDrawCall;
		Text* m_debugInfoDrawCmd;
#endif // _DEBUG
	private:
		friend class Application;
	};
}


#endif // GameWorld_h__
